package com.qingzhuge.scheduler.service;

import com.qingzhuge.scheduler.entity.SysTaskScheduled;
import com.qingzhuge.scheduler.dto.request.TaskJobKey;
import com.qingzhuge.scheduler.dto.respone.SysTaskScheduledResponseDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author : zero.xiao
 * @description :
 * @date :2019-05-17 11:39
 * @modified :
 */
@Slf4j
@Component
public class SchedulerServiceImpl implements ISchedulerService {
    @Autowired
    private SchedulerManager schedulerManager;


    /** 获取所有作业
     * */
    @Override
    public List<SysTaskScheduledResponseDto> getAllTaskDetail() {
        return schedulerManager.getAllJobDetail();
    }

    /**
     * 暂停所有触发器
     */
    @Override
    public void pauseAllTrigger() {
        schedulerManager.pauseAllTrigger();
    }

    /**
     * 启动所有触发器
     */
    @Override
    public void startAllTrigger() {
        schedulerManager.startAllTrigger();
    }

    /** 执行作业*/
    @Override
    public void execTask(TaskJobKey jobKey) {
        schedulerManager.runJob(jobKey);
    }

    /** 恢复作业*/
    @Override
    public void openTask(TaskJobKey jobKey) {
        schedulerManager.resumeJob(jobKey);
    }

    /** 暂停作业*/
    @Override
    public void stopTask(TaskJobKey jobKey) {
        schedulerManager.stopJob(jobKey);
    }

    /** 删除作业*/
    @Override
    public void delTask(TaskJobKey jobKey) {
        schedulerManager.delJob(jobKey);
    }

    /** 新增/修改任务*/
    @Override
    public void task(SysTaskScheduled taskScheduled) {
        schedulerManager.task(taskScheduled);
    }

    @Override
    public TaskJobKey getTaskJobKey(SysTaskScheduled scheduled) {
        if (StringUtils.isEmpty(scheduled.getTaskName()) && StringUtils.isEmpty(scheduled.getTaskGroup())){
            return null;
        }
        return new TaskJobKey(scheduled.getTaskName(), scheduled.getTaskGroup());
    }

}